package org.dormi.learn;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * 先 package 一下, 把 02_res/target/abc.jar
 * 自定义的加载器 加载 jar中的class
 * getDeclaredMethods includes all methods declared by the class itself,
 * getMethods returns only public methods, but also those inherited from a base class
 *
 * @author : dormi330
 * @since : 22/04/21
 */
public class CustomClassLoader01 {
    public static void main(String[] args) throws Exception {
        File file = new File("E:\\workspace\\java_basic\\class_loader\\02_res\\target\\abc.jar");
        URL[] urls = new URL[]{file.toURI().toURL()};
        URLClassLoader loader1 = new URLClassLoader(urls);

        // 要确保本module 内没有 foo.bar.Demo01
        Class<?> clazz = loader1.loadClass("foo.bar.Demo01");
        System.out.println(clazz.getClassLoader());
        Object obj = clazz.newInstance();
        Method method = clazz.getMethod("hello");
        method.invoke(obj);
    }
}
/*
java.net.URLClassLoader@2db0f6b2
hello
*/
